package fx;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.file.FileReader;
import fx.common.Constants;
import fx.stage.StockShow;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransparentForm extends Application {

    public static Map<String, String> stock = new HashMap<>();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws UnsupportedEncodingException {
        // 从配置文件读取股票信息配置
        String configFile = this.getConfigFilePath();
        String fileName = configFile + Constants.configFileName;
        FileReader fileReader = new FileReader(fileName);
        List<String> stockInfo = fileReader.readLines();
        if (CollectionUtil.isEmpty(stockInfo)) {
            stock.put("601919", "中远海控");
        } else {
            for (String stockSting : stockInfo) {
                String[] split = stockSting.split(Constants.splitChar);
                stock.put(split[1], split[0]);
            }
        }

    }

    @Override
    public void start(final Stage mainStage) throws Exception {

        StockShow stockShow = new StockShow();

        // 获取股票展示窗口
        Stage primaryStage = stockShow.getShowStage();
        // 将 primaryStage 设置为归属对象，即父级窗口
        primaryStage.initOwner(mainStage);
        // 设置风格为 UTILITY
        mainStage.initStyle(StageStyle.UTILITY);
        // 设置父级透明度为0
        mainStage.setOpacity(0);
        mainStage.show();
        primaryStage.show();

        // 保证窗口关闭后，Stage对象仍然存活
        Platform.setImplicitExit(false);
    }

    private String getConfigFilePath() throws UnsupportedEncodingException {
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
        path = java.net.URLDecoder.decode(path, "UTF-8");
        int firstIndex = path.indexOf(Constants.fileSplit) + 1;
        int lastIndex = path.lastIndexOf(Constants.fileSplit) - 3;
        return path.substring(firstIndex, lastIndex);
    }
}
